﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;
using System.Web;
using WmsService.Derived;
using WmsService.Exceptions;
using WmsService.In;
using WmsService.Out;
using WmsService.Security;

namespace WmsService
{

    public partial class Service
    {
        #region Security
        public string Login(string userID, string password)
        {
            if (userID == null) throw new ArgumentNullException("userID");
            if (password == null) throw new ArgumentNullException("password");
            return AuthenticationFactory.GetAuthentication().Login(userID, password);
        }

        public string ChangePassword(string userID, string oldPassword, string newPassword)
        {
            if (userID == null) throw new ArgumentNullException("userID");
            if (oldPassword == null) throw new ArgumentNullException("oldPassword");
            if (newPassword == null) throw new ArgumentNullException("newPassword");
            try
            {
                SecurityValidate();
                var user = GetSystemUser(userID);
                if (user == null)
                {
                    Exceptions.ExceptionHandler.ThrowException("不存在用户ID：" + userID);
                }
                if (user.PASSWORD != oldPassword)
                {
                    Exceptions.ExceptionHandler.ThrowException("用户密码错误！");
                }
                user.PASSWORD = newPassword;
                user.WcfEntityState = EntityState.Modified;
                SaveSystemUsers(new List<SystemUser>() { user });
                return "修改成功！";
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public string SetPassword(string userID, string newPassword)
        {
            if (userID == null) throw new ArgumentNullException("userID");
            try
            {
                SecurityValidate();
                var user = GetSystemUser(userID);
                if (user == null)
                {
                    Exceptions.ExceptionHandler.ThrowException("不存在用户ID：" + userID);
                }
                user.PASSWORD = newPassword;
                user.WcfEntityState = EntityState.Modified;
                SaveSystemUsers(new List<SystemUser>() { user });
                return "设置密码成功！";
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public SystemUser GetSystemUser(string userID)
        {
            if (userID == null) throw new ArgumentNullException("userID");
            try
            {
                SecurityValidate();
                using (var ctx = Factory.ContextFactory.Create())
                {
                    var q = from c in ctx.SystemUser
                            where c.USERID == userID
                            select c;
                    List<SystemUser> list = q.ToList();
                    if (list.Count == 0)
                        return null;
                    ctx.Detach(list[0]);
                    return list[0];
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        #endregion

    }
}
